<template>
    <el-dialog :visible.sync="showDialog" :title="$t('salesManage.sendMsg')" @close="close" width="460px">
        <el-form ref="sendForm" :model="msgForm" :rules="rules">
            <el-form-item :label="$t('common.mobileNumber')" prop="phone"><!--手机号码-->
                <el-input v-model="msgForm.phone" :placeholder="$t('common.mobileNumber')" maxlength="11"></el-input>
            </el-form-item>
            <el-form-item :label="$t('common.SMSContent')" prop="content"><!--短信内容-->
                <el-input v-model="msgForm.content" :placeholder="$t('common.SMSContent')" :rows="5" maxlength="200" type="textarea"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="send" type="primary">{{ $t('common.send') }}</el-button><!--发送-->
            <el-button @click="close">{{ $t('common.cancel') }}</el-button><!--取消-->
        </div>
    </el-dialog>
</template>

<script>
    // import EventBus from '@/libs/event-bus';

    export default {
        name: 'send-message',
        props: {
            visible: {
                type: Boolean,
                required: true,
                default: false
            },
            type: {
                type: String,
                required: true,
            },
            phone: {
                type: String,
                required: true,
            },
            appName: {
                type: String,
            }
        },
        data() {
            return {
                showDialog: false,
                msgForm: {
                    phone: '',
                    content: '',
                },
                rules: {
                    phone: [
                        { required: true, message: this.$t('verify.plsInput') }, // 请输入
                        { pattern: /^09[0-9]{9}$/, message: this.$t('verify.plsInputRightPhone') }, // 请填写正确的09开头11位数字号码
                    ],
                    content: [
                        { required: true, message: this.$t('verify.plsEnterSMSContent') }, // 短信内容不能为空
                        { pattern: /^(?!\s+$).+/, message: this.$t('verify.plsEnterSMSContent') }, // 短信内容不能为空
                    ],
                }
            }
        },
        watch: {
            visible (newVal) {
                // console.log(newVal);
                this.showDialog = newVal;
                if (newVal) {
                    this.msgForm.phone = this.phone;
                    this.msgForm.content = '';
                    this.$nextTick(() => {
                        this.$refs.sendForm.clearValidate();
                    });
                }
            },
        },
        methods: {
            send() {
                const user = this.$storage.get('user');
                const { phone, content } = this.msgForm;
                const { batchCode } = this.$route.query;
                const { telUserId } = this.$route.params;
                const appName = this.appName.includes(',') ? this.appName.split(',')[0] : this.appName
                this.$refs.sendForm.validate(val => {
                    if (val) {
                        const params = {
                            phone,
                            content,
                            smsWay: this.type, // audit（信审） collection(催收)
                            operator: user.username,
                            userId: user.id,
                            batchCode,
                            telUserId,
                            appName: appName,
                        };
                        // header里面需有X-LG-CompanyName、 X-LG-Domain、token
                        const headers = {
                            'X-LG-CompanyName': user.companyName,
                            // 'X-LG-Domain': user.domain
                        };
                        this.$api.sms.manualSend(params, { headers }).then(res => {
                            const { status } = res;
                            if ('1' === status) {
                                // this.$emit('updateTable', 'msgRecords');
                                // EventBus.$emit('sendMsgRecords', 'update');
                                this.$message.success(this.$tc('common.sendSuccess'));
                                this.close();
                            }
                        });
                    }
                });
            },
            // 关闭弹框
            close() {
                this.showDialog = false;
                this.$emit('update:visible', false);
            },
        },
        created() {
            this.showDialog = this.visible;
            this.msgForm.phone = this.phone;
        }
    }
</script>

<style scoped>

</style>
